package kr.hkit.androidcomplete02.service;

import kr.hkit.androidcomplete02.R;
import kr.hkit.androidcomplete02.service.LocalService.LocalBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class LocalServiceTest extends Activity { //localService에 있는 메소드 호출한것
	private LocalService mService;
	private boolean mBound;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.service_localservicetest);
	}
	
	
	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = new Intent("kr.hkit.androidcomplete02.service.LOCALSERVICE");
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if (mBound) { //true라면. 연결되있다면
			unbindService(mConnection);
			mBound = false;
		}
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			mBound = false;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			//연결될 때
			mService = ((LocalBinder)service).getService();
			mBound = true;
		}
	};
	
	
	public void mOnClick(View v) {
		if (v.getId()==R.id.bound_req && mBound){ //mBound가 true라면
			//String text = mService.getRandomNumber()+"";
			String text = mService.getRandomNumber(5, 9)+""; //추가가능
			//LocalService에 만들어놓고 쓸수있음
			Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		}
	}

}
